$(function () {
    $.ajax({
        url: "/cart/getCart",
        data: { uid: JSON.parse(sessionStorage.getItem("userInfo")).uId }
    }).then(function (result) {
        let { data } = result;

        let strHtml = ``
        data.forEach(item => {
            strHtml += `
            <ul class="listBox clearfix">
                    <li class="pad_t20">
                        <input type="checkbox" class="ed">
                    </li>
                    <li class="man1_pro ">
                        <a href="javascript:void(0)">
                            <img src="${item.cImg}" alt="">
                        </a>
                    </li>
                    <li class="man1_n">
                        <p class="man1_name">
                            <a href="javascript:void(0)">${item.cName}</a>
                        </p>
                    </li>
                    <li class="man1_p_m ">
                        <div class="price_font">
                            ¥${item.cPrice}
                        </div>
                    </li>
                    <li class="man1_p_m add jj">
                        <div class="box">
                            <button class="pre">-</button>
                            <input type="text" value="${item.cNumber}">
                            <button class="bre">+</button>
                        </div>
                    </li>
                    <li class="man1_p_m price_font">
                        ¥${item.cNumber*item.cPrice}
                    </li>
                    <li class="man1_p_m main1_del">
                        <a href="javascript:void(0)" data-cid=${item.cId}>【删除】</a>
                        
                    </li>
                </ul>
            
            `
        })

        $(".man1_con").html(strHtml)
    })
})


$(function(){
    $(".man1_con").on("click",".box",function(){
        console.log(1111);
        var num = $(this).next().val();
        if (num <= 1) {
            return;
        }
        num--;
        $(this).next().val(num);
        
    })
   
})

//删除
console.log($(".main1_del a"));
$(function () {
    $(".man1_con").on("click", ".main1_del a", function () {
        console.log(1111);
        // data-cid
        let self = $(this);
        $.ajax({
            url: "/cart/delete",
            type: "post",
            data: {
                cId: $(this).data("cid")
            }
        }).then(function (res) {
            if (res.code == 200) {
                self.parents("ul").remove();
            }

        })
    })


})

// 全选
$(function(){
    $(".allq").click(function(){
        $(".man1_con :checkbox").prop("checked",$(this).prop("checked"))
    })
    $(".man1_con").on("click",".ed",function(){
        let cbxCount = $(".ed").length
        let checkCount = $(".ed:checked").length
        $(".allq").prop("checked",cbxCount==checkCount)
    })
    console.log($(".ed"));
})